<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\SysItem;
use backend\models\AfterService;
use backend\models\AdminInfo;
use yii\db\ActiveRecord;
use yii\data\Sort;
use frontend\models\UserFavourite;

/**
 * SysItemSearch represents the model behind the search form about `backend\models\SysItem`.
 */
class UserFootSearch extends UserFoot
{
    /**
     * @inheritdoc
     * 
     */
	 
		 public function rules()
		    {    	
		        return [
		            [['show_count'], 'integer'],
		        		[['start_time','end_time'], 'safe'],
		          	['end_time', 'validataTime'],
		        ];
		    }
		    public function attributeLabels()
		    {
		    	return [
		    			'show_count' => Yii::t('app','记录数'),
		    			'start_time' => Yii::t('app','开始日期'),
		    			'end_time' => Yii::t('app','结束日期'),
		    			'type' => Yii::t('app','类别'),
		    	];
		    }
		    /**
		     *  
		     * @param unknown $attribute
		     */
		    public function validataTime($attribute) {
		    	if ($this->end_time < $this->start_time) {
		    		$this->addError($attribute, '结束时间不可小于开始时间');
		    	}
		    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
    	$today=date("y-m-d",time());
    	$this->start_time=date("Y-m-d",strtotime("$today -1month"));
    	$this->end_time=date('Y-m-d');
    	$this->load($params);
    	$show_count=30;    
    	$condition=" action=:action  and calltime>=:start_time and calltime<=:end_time";//默认查询单品的30条
    	$starttime=strtotime(date("Y-m-d",strtotime("$today -1month")));//本月初
    	$endtime=strtotime(time()); 
    	$t_table="item";
    	if(array_key_exists("UserFootSearch", $params)){
    		//类别过滤
    		if(array_key_exists("type", $params['UserFootSearch'])){
    			if(intval($params['UserFootSearch']['type'])>0){
    				$type=intval($params['UserFootSearch']['type']);
    				switch ($type){
    					case 1:
    						$t_table="item";
    						break;
    					case 2:
    						$t_table="set";
    						break;
    					case 3:
    						$t_table="show";
    						break;
    					default: 
    						$t_table="item";
    						break;
    				}
    				$this->type=$type;
    			} 
    		} 
    		//总记录数过滤
    		if(array_key_exists("show_count", $params['UserFootSearch'])){
    			$dis_count=intval($params['UserFootSearch']['show_count']);
    			if($dis_count>0){
    				$show_count=$dis_count;
    			} 
    		}
    		//开始时间
    		if(array_key_exists("start_time", $params['UserFootSearch'])){
    			if(intval($params['UserFootSearch']['start_time'])>0){
    				$starttime=strtotime($params['UserFootSearch']['start_time']);
    			}
    		}
    		//结束时间
    		if(array_key_exists("end_time", $params['UserFootSearch'])){
    			if(intval($params['UserFootSearch']['end_time'])>0){
    				$endtime=strtotime($params['UserFootSearch']['end_time']);
    			}
    		}
    	}
    	switch ($t_table){
    		case "item":
    			$sql="SELECT action_id,title,sys_item.id,sys_item.name,action,COUNT(log_user_footprint.id) visitor_num ,sys_item.`img_small_id`,sys_image.`url` img 
    			FROM log_user_footprint
    			LEFT JOIN sys_item  ON log_user_footprint.`action_id`=sys_item.`id`
    			LEFT JOIN sys_image ON sys_item.`img_small_id`=sys_image.`id`
    			where  $condition  GROUP BY action_id   order by visitor_num desc  LIMIT :count ";
    			break;
    		case "set":
    			$sql="SELECT action_id,title,taozhuang.sid id,taozhuang.name,action,COUNT(log_user_footprint.id) visitor_num ,taozhuang.`img_small_id` ,sys_image.`url` img 
    			FROM log_user_footprint
    			LEFT JOIN taozhuang  ON log_user_footprint.`action_id`=taozhuang.`sid`
    			LEFT JOIN sys_image ON taozhuang.`img_small_id`=sys_image.`id`
    			where  $condition  GROUP BY action_id   order by visitor_num desc   LIMIT :count ";
    			break;
    		case "show":
    			$sql="SELECT action_id,title,show.id,show.name,action,COUNT(log_user_footprint.id) visitor_num ,`show`.`img_small_id` ,sys_image.`url` img 
    			FROM log_user_footprint
				LEFT JOIN `show`  ON log_user_footprint.`action_id`=`show`.id
    			 LEFT JOIN sys_image ON `show`.`img_small_id`=sys_image.`id`
				where  $condition  GROUP BY action_id   order by visitor_num desc   LIMIT :count ";
    			break;
    		default:
    			$sql="SELECT action_id,title,sys_item.id,sys_item.name,action,COUNT(log_user_footprint.id) visitor_num ,sys_item.`img_small_id` ,sys_image.`url` img 
    			FROM log_user_footprint  
    			LEFT JOIN sys_item  ON log_user_footprint.`action_id`=sys_item.`id`
    			LEFT JOIN sys_image ON sys_item.`img_small_id`=sys_image.`id`
    			where  $condition  GROUP BY action_id   order by visitor_num desc  LIMIT :count ";
    			break;
    	}
    	$conParam=array(":count"=>$show_count,":action"=>$t_table,":start_time"=>$starttime,":end_time"=>$endtime);
    	$query=UserFoot::findBySql($sql,$conParam);
    	
    	$sql="   SELECT SUM(visitor_num)      FROM (SELECT COUNT(id) visitor_num  FROM log_user_footprint
    			WHERE $condition  GROUP BY action_id   ORDER BY visitor_num DESC  LIMIT :count 
             LIMIT :count  ) c;";
    	$acount=UserFoot::findBySql($sql,$conParam)->column()[0];
    	/* $sql=$query->createCommand()->getRawSql();
    	return $sql; */
    	$dataProvider = new ActiveDataProvider([
    			'query' => $query,
    		'pagination'=>["pageSize"=>$show_count],
    			 'sort'=>false
    	]);
        return array("dataProvider"=>$dataProvider,"acount"=>$acount); 
    }
    
}
